/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Slave;

import Core.ConnectionHandler;
import Core.MessageBuilder;
import Core.MessageType;
import org.openymsg.network.event.SessionEvent;

/**
 *
 * @author Mr Bao
 */
public class SessionAdapter extends org.openymsg.network.event.SessionAdapter {

    private ConnectionHandler connectionHandler;

    public SessionAdapter(ConnectionHandler connectionHandler) {
        this.connectionHandler = connectionHandler;
    }

    @Override
    public void messageReceived(SessionEvent event) {
        String from = event.getFrom();
        String to = event.getTo();
        String message = event.getMessage();
        String messageResult = MessageBuilder.BuildFriendReplyMessage(from, to, message);
        connectionHandler.QueueMessage(messageResult);
    }

    @Override
    public void connectionClosed(SessionEvent event) {
        String from = event.getFrom();
        String message = event.getMessage();
        connectionHandler.QueueMessage(from + MessageBuilder.TWO + message);
    }

    @Override
    public void buzzReceived(SessionEvent event) {
        String from = event.getFrom();
        String to = event.getTo();
        String message = event.getMessage();
        String messageToClient = MessageType.SendBuzz;
        messageToClient += MessageBuilder.ONE;
        messageToClient += from;
        messageToClient += MessageBuilder.TWO;
        messageToClient += message;
        connectionHandler.QueueMessage(messageToClient);
    }
}
